"""
Run integration tests.

These tests connect to an object server and do stuff. Make sure it's
not an object server you care about.
"""
import sys
import tests

import unittest
from testutil import run_tests

loader = unittest.TestLoader()

if __name__ == "__main__":
    args = sys.argv[1:]
    if len(args) < 1:
        print "Need an object server name"
        sys.exit(2)

    tests._test_objserv_host = args[0]

    if len(args) < 2:
        # no args - run all tests
        tests = tests.all_tests
    else:
        # only run specific tests
        parts = args[1].split(".")
        if sys.modules.has_key(".".join(parts[:-2])):
            # given method and class
            tmod = ".".join(parts[:-2])
            tname = ".".join(parts[-2:])
            tests = loader.loadTestsFromName(tname, sys.modules[tmod])
        elif sys.modules.has_key(".".join(parts[:-1])):
            # given just class
            tmod = ".".join(parts[:-1])
            tname = parts[-1:]
            tests = loader.loadTestsFromName(tname, sys.modules[tmod])
        else:
            print "Need module, class and (optionally) method"
            sys.exit(2)

    run_tests(tests)
